﻿using System;

namespace WgpgGUI
{
    public static class PassphraseStrengthChecker
    {
        /// <summary>
        /// This methods returns a value between 0 and 100 which indicates
        /// whether a passphrase is good or not.
        /// 0 = not good
        /// 100 = very good
        /// </summary>
        /// <param name="passphrase">The passphrase to test</param>
        /// <returns>A value between 0 and 100</returns>
        public static Int32 Strength(String passphrase)
        {
            Int32 val = 0;

            val += HasUpperCase(passphrase) ? 1 : 0;
            val += HasLowerCase(passphrase) ? 1 : 0;
            val += HasNumber(passphrase) ? 1 : 0;
            val += HasSymbol(passphrase) ? 1 : 0;
            val += Has8Chars(passphrase) ? 1 : 0;

            return val * 20;
        }

        // ----------------------------------------------------------------------------------------

        private static Boolean HasUpperCase(String passphrase)
        {
            foreach (Char c in passphrase)
                if (Char.IsUpper(c) && Char.IsLetter(c))
                    return true;
            return false;
        }

        private static Boolean HasLowerCase(String passphrase)
        {
            foreach (Char c in passphrase)
                if (Char.IsLower(c) && Char.IsLetter(c))
                    return true;
            return false;
        }

        private static Boolean HasNumber(String passphrase)
        {
            foreach (Char c in passphrase)
                if (Char.IsDigit(c))
                    return true;
            return false;
        }

        private static Boolean HasSymbol(String passphrase)
        {
            foreach (Char c in passphrase)
                if (!Char.IsLetterOrDigit(c))
                    return true;
            return false;
        }

        private static Boolean Has8Chars(String passphrase)
        {
            return passphrase.Length >= 8;
        }
    }
}
